/**
 ******************************************************************************
 * @file    cmt.c
 * @author  hyseim software Team
 * @date    18-Mar-2024
 * @brief   This file provides all the tmp functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "cmt.h"

/** @addtogroup IM110GW_Driver
 * @{
 */

/** @defgroup CMT
 * @brief CMT driver modules
 * @{
 */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup CMT_Private_Functions
  * @{
  */

/**
 * @brief  Enable CMT Timerx counter.
 * @param  CMTx: Pointer to selected CMT peripheral.
 *         This parameter can be one of the following values:
 *         CMT0, CMT1.
 * @param  CMT_TimerNum: The serial number of the CMT timer.
 * @return None
 */
void CMT_Timer_Enable(CMT_t* CMTx, uint8_t CMT_TimerNum)
{
    CMTx->Timer[CMT_TimerNum].CONTROL |= CMT_TIMER_CONTROL_EN;
}

/**
 * @brief  Disable CMT Timerx counter.
 * @param  CMTx: Pointer to selected CMT peripheral.
 *         This parameter can be one of the following values:
 *         CMT0, CMT1.
 * @param  CMT_TimerNum: The serial number of the CMT timer.
 * @return None
 */
void CMT_Timer_Disable(CMT_t* CMTx, uint8_t CMT_TimerNum)
{
    CMTx->Timer[CMT_TimerNum].CONTROL &= ~CMT_TIMER_CONTROL_EN;
}

/**
 * @brief  Sets the CMT Timerx counter load value.
 * @param  CMTx: Pointer to selected CMT peripheral.
 *         This parameter can be one of the following values:
 *         CMT0, CMT1.
 * @param  CMT_TimerNum: The serial number of the CMT timer.
 * @param  LoadValue: CMT Timerx load new value.
 * @return None
 */
void CMT_Timer_SetCounterLoad(CMT_t* CMTx, uint8_t CMT_TimerNum, uint32_t LoadValue)
{
    CMTx->Timer[CMT_TimerNum].LOADCOUNT = LoadValue;
}

/**
 * @brief  Initialize CMT Timerx.
 * @param  CMTx: Pointer to selected CMT peripheral.
 *         This parameter can be one of the following values:
 *         CMT0, CMT1.
 * @param  CMT_TimerNum: The serial number of the CMT timer.
 * @param  CountMode: CMT Timerx count mode.
 * @param  IntMask: CMT Timerx interrupt mask.
 * @param  LoadValue: CMT Timerx load value.
 * @return None
 */
void CMT_Timer_Init(CMT_t* CMTx, uint8_t CMT_TimerNum, uint8_t CountMode, uint8_t IntEnable, uint32_t LoadValue)
{
    CMT_Timer_SetCounterLoad(CMTx, CMT_TimerNum, LoadValue);

    CMTx->Timer[CMT_TimerNum].CONTROL = CountMode | IntEnable;
}

/**
 * @brief  Gets the current count value of CMT Timerx.
 * @param  CMTx: Pointer to selected CMT peripheral.
 *         This parameter can be one of the following values:
 *         CMT0, CMT1.
 * @param  CMT_TimerNum: The serial number of the CMT timer.
 * @return CMT Timerx current counter value
 */
uint32_t CMT_Timer_GetCounter(CMT_t* CMTx, uint8_t CMT_TimerNum)
{
    return CMTx->Timer[CMT_TimerNum].CURRENTVALUE;
}

/**
 * @brief  Checks whether the specified CMT Timerx interrupt has occurred or not.
 * @param  CMTx: Pointer to selected CMT peripheral.
 *         This parameter can be one of the following values:
 *         CMT0, CMT1.
 * @param  CMT_TimerNum: The serial number of the CMT timer.
 * @return The new state of the CMT Timerx_INTSTATUS (SET or RESET)
 */
FlagStatus_t CMT_Timer_GetINTStatus(CMT_t* CMTx, uint8_t CMT_TimerNum)
{
    ITStatus_t bitstatus = RESET;
    bitstatus = CMTx->Timer[CMT_TimerNum].INTSTATUS & CMT_TIMER_CONTROL_INTSTATUS;
    return bitstatus;
}

/**
 * @brief  Clears the CMT Timerx pending flags.
 * @param  CMTx: Pointer to selected CMT peripheral.
 *         This parameter can be one of the following values:
 *         CMT0, CMT1.
 * @param  CMT_TimerNum: The serial number of the CMT timer.
 * @return None
 */
void CMT_Timer_ClearFlag(CMT_t* CMTx, uint8_t CMT_TimerNum)
{
    CMTx->Timer[CMT_TimerNum].EOI = 0x0;
}

/**
  * @}
  */

/**
 * @}
 */

/**
 * @}
 */
